package com.ita.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.ita.dao.MenuDao;
import com.ita.pojo.Menu;
import com.ita.util.DBUtil;


public class MenuDaoImpl implements MenuDao{
	
	private Logger logger = Logger.getLogger(MenuDaoImpl.class);

	@Override
	public int addMenu(Menu menu) {
		Connection connection = null;
		PreparedStatement pst = null;
		int m = 0;
		try {
			String sql = "insert into food(id,name,price,img,type_id,m_id) values(?,?,?,?,?,?)";
			connection = DBUtil.createConnection();
			pst = connection.prepareStatement(sql);
			pst.setString(1, menu.getId());
			pst.setString(2, menu.getName());
			pst.setDouble(3, menu.getPrice());
			pst.setString(4, menu.getImg());
			pst.setString(5, menu.getType());
			pst.setString(6, menu.getMid());
			m = pst.executeUpdate();
			return m;
		} catch (Exception e) {
			logger.error(e.getMessage());
			return m;
		} finally {
			DBUtil.close(connection, pst, null);
		}
	}

	@Override
	public int deleteMenu(String id, String m_id) {
		String sql = "delete from food where id= ? and m_id = ?";
		Connection connection = null;
		PreparedStatement pst = null;
		try {
			connection = DBUtil.createConnection();
			pst = connection.prepareStatement(sql);
			pst.setString(1, id);
			pst.setString(2, m_id);
			pst.executeUpdate();
			return 1;
		} catch (SQLException e) {
			System.out.println(e.getMessage());
			return 0;
		} finally {
			DBUtil.close(connection, pst, null);
		}
	}

	@Override
	public int updateMenu(Menu menu) {
		String sql = "update food set name=?,price=?,img=?,type_id=? where id=? and m_id =? ";
		Connection connection = null;
		PreparedStatement pst = null;
		try {
			connection = DBUtil.createConnection();
			pst = connection.prepareStatement(sql);
			pst.setString(1, menu.getName());
			pst.setDouble(2, menu.getPrice());
			pst.setString(3, menu.getImg());
			pst.setString(4, menu.getType());
			pst.setString(5, menu.getId());
			pst.setString(6, menu.getMid());
			pst.executeUpdate();
			return 1;
		} catch (SQLException e) {
			System.out.println(e.getMessage());
			return 0;
		} finally {
			DBUtil.close(connection, pst, null);
			
		}
	}

	@Override
	public Menu loadMenu(String id, String m_id) {
		String sql = "select f.*,t.type typename from food f join food_type t on t.id = f.type_id where f.m_id=? and f.id =?";
		Connection connection = null;
		PreparedStatement pst = null;
		Menu food = null;
		ResultSet rs = null;
		try {
			connection = DBUtil.createConnection();
			pst = connection.prepareStatement(sql);
			pst.setString(1, m_id);
			pst.setString(2, id);
			rs = pst.executeQuery();
			while (rs.next()) {
				food = new Menu();
				food.setId(rs.getString("id"));
				food.setName(rs.getString("name"));
				food.setPrice(rs.getDouble("price"));
				food.setImg(rs.getString("img"));
				food.setType(rs.getString("type_id"));
				food.setTypeName(rs.getString("typename"));
				food.setMid(rs.getString("m_id"));
			}
			return food;
		} catch (SQLException e) {
			logger.equals(e.getMessage());
			return food;
		} finally {
			DBUtil.close(connection, pst, rs);
		}
	}
	
	@Override
	public List<Menu> loadMenu(String type) {
		String sql = "select f.*,t.type typename from food f join food_type t on t.id = f.type_id  where f.type_id=?";
		Connection connection = null;
		PreparedStatement pst = null;
		Menu food = null;
		ResultSet rs = null;
		List<Menu> list = new ArrayList<Menu>();
		try {
			connection = DBUtil.createConnection();
			pst = connection.prepareStatement(sql);
			pst.setString(1, type);
			rs = pst.executeQuery();
			while (rs.next()) {
				food = new Menu();
				food.setId(rs.getString("id"));
				food.setName(rs.getString("name"));
				food.setImg(rs.getString("img"));
				food.setPrice(rs.getDouble("price"));
				food.setType(rs.getString("type_id"));
				food.setTypeName(rs.getString("typename"));
				food.setMid(rs.getString("m_id"));
				list.add(food);
			}
			return list;
		} catch (SQLException e) {
			logger.equals(e.getMessage());
			return list;
		} finally {
			DBUtil.close(connection, pst, rs);
		}
	}

	@Override
	public List<Menu> findAll(String m_id) {
		String sql = "select f.*,t.type typename from food f join food_type t on f.type_id = t.id where f.m_id = ?";
		Connection connection = null;
		PreparedStatement pst = null;
		Menu food = null;
		List<Menu> list = new ArrayList<Menu>();
		ResultSet rs = null;
		try {
			connection = DBUtil.createConnection();
			pst = connection.prepareStatement(sql);
			pst.setString(1, m_id);
			rs = pst.executeQuery();
			while (rs.next()) {
				food = new Menu();
				food.setId(rs.getString("id"));
				food.setName(rs.getString("name"));
				food.setImg(rs.getString("img"));
				food.setPrice(rs.getDouble("price"));
				food.setType(rs.getString("type_id"));
				food.setTypeName(rs.getString("typename"));
				food.setMid(rs.getString("m_id"));
				list.add(food);
			}
			return list;
		} catch (SQLException e) {
			logger.equals(e.getMessage());
			return list;
		} finally {
			DBUtil.close(connection, pst, rs);
		}
	}

}
